class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int> > result;
        queue<TreeNode*> q;
        if(root) {
            q.push(root);
        }
        while(!q.empty()) {
            int size = q.size();
            vector<int> v;
            for(int i = 0; i < size; i++) {
                TreeNode* node = q.front();
                q.pop();
                v.push_back(node->val);
                if(node->left) {
                    q.push(node->left);
                }
                if(node->right) {
                    q.push(node->right);
                }
            }
            result.push_back(v);
        }
        return result;
    }
};